//decompositions



//Three categories: 
*demographic: race, marriage/fertility profiles, parent distribution, population
*economic: taxes, wages, cost of living, other amenities
*scholastic: student-teacher ratios, govt spending, college tuition

//economic factors: taxes, wages, cost of living
local years `"2000 2010"'
foreach year in `years'{
	import delimited "$dir/Model/utilities/state_chars_`year'.csv", clear

	su v2
	replace v2 = `r(mean)'

	su v3
	replace v3 = `r(mean)'

	su v9
	replace v9 = `r(mean)'

	su v6 
	replace v6 = `r(mean)'
	export delimited "$dir/Model/utilities/state_chars_`year'_decomp_e.csv", novarn replace
}

//scholastic: student-teacher ratios, govt spending, college tuition
local years `"2000 2010"'
foreach year in `years'{
	import delimited "$dir/Model/utilities/state_chars_`year'.csv", clear

	su v4
	replace v4 = `r(mean)'

	su v5
	replace v5 = `r(mean)'


	su v8
	replace v8 = `r(mean)'

	export delimited "$dir/Model/utilities/state_chars_`year'_decomp_s.csv", novarn replace
}

*****demographic: race, marriage/fertility profiles, parent distribution, population****
*this one takes a bit more work!

**************population******************
local years `"2000 2010"'
foreach year in `years'{
	import delimited "$dir/Model/utilities/state_chars_`year'.csv", clear

	su v11
	replace v11 = `r(mean)'

	export delimited "$dir/Model/utilities/state_chars_`year'_decomp_d.csv", novarn replace
}


*************marriage probabilities--type distribution done elsewhere. Do the rest here.******************
use "$temp/acs_master_kid_sample", clear

levelsof statefip, clean local(fips)
gen married=0
replace married = 1 if marst == 1 | marst == 2
gen mig = (statefips!=bpl)

preserve
probit married hc* mig if hc<3.0 & !coll [fw = perwt], robust 
local cons = _b[_cons]
local hc = _b[hc]
local hc2 = _b[hc2]
local hc3 = _b[hc3]
local mig = _b[mig]
qui su married if hc>3.0 & !coll [fw=perwt]
local cap = `r(mean)'

foreach fip in `fips'{
	clear
	set obs 1
	gen fips = `fip'
	gen cons = `cons'
	gen b1 = `hc'
	gen b2 = `hc2'
	gen b3 = `hc3'
	gen cap = `cap'
	gen mig = `mig'
	save "$temp/marriage_decomp_ncoll_`fip'", replace
}

clear
foreach fip in `fips'{
	append using "$temp/marriage_decomp_ncoll_`fip'"
}
export delimited "$dir/Model/utilities/kid_marriage_probs_ncoll_decomp_d.csv", replace novarn
restore

probit married hc* mig if hc<3.0 & coll [fw = perwt], robust 
local cons = _b[_cons]
local hc = _b[hc]
local hc2 = _b[hc2]
local hc3 = _b[hc3]
local mig = _b[mig]
qui su married if hc>3.0 & coll [fw=perwt]
local cap = `r(mean)'

foreach fip in `fips'{
	clear
	set obs 1
	gen fips = `fip'
	gen cons = `cons'
	gen b1 = `hc'
	gen b2 = `hc2'
	gen b3 = `hc3'
	gen cap = `cap'
	gen mig = `mig'
	save "$temp/marriage_decomp_coll_`fip'", replace
}

clear
foreach fip in `fips'{
	append using "$temp/marriage_decomp_coll_`fip'"
}
export delimited "$dir/Model/utilities/kid_marriage_probs_coll_decomp_d.csv", replace novarn

***************fertility probabilities*****************
use "$temp/acs_master_kid_sample", clear
keep if age<=45 //restrict to avoid mechanically reducing fertility rate
gen married=0
replace married = 1 if marst == 1 | marst == 2
gen kid = (nchild>0) //recode kid

//college, married
preserve
probit kid hc* if hc<3.0 & coll & married [fw = perwt], robust 
local cons = _b[_cons]
local hc = _b[hc]
local hc2 = _b[hc2]
local hc3 = _b[hc3]
qui su kid if hc>3.0 & coll & married [fw=perwt]
local cap = `r(mean)'

foreach fip in `fips'{
	clear
	set obs 1
	gen fips = `fip'
	gen cons = `cons'
	gen b1 = `hc'
	gen b2 = `hc2'
	gen b3 = `hc3'
	gen cap = `cap'
	gen mig = `mig'
	save "$temp/fert_decomp_coll_marr_`fip'", replace
}

clear
foreach fip in `fips'{
	append using "$temp/fert_decomp_coll_marr_`fip'"
}

export delimited "$dir/Model/utilities/kid_fertility_probs_marr_coll_decomp_d.csv", replace novarn
restore

//college, not married
preserve
probit kid hc* if hc<3.0 & coll & !married [fw = perwt], robust 
local cons = _b[_cons]
local hc = _b[hc]
local hc2 = _b[hc2]
local hc3 = _b[hc3]
qui su kid if hc>3.0 & coll & !married [fw=perwt]
local cap = `r(mean)'

foreach fip in `fips'{
	clear
	set obs 1
	gen fips = `fip'
	gen cons = `cons'
	gen b1 = `hc'
	gen b2 = `hc2'
	gen b3 = `hc3'
	gen cap = `cap'
	gen mig = `mig'
	save "$temp/fert_decomp_coll_nmarr_`fip'", replace
}

clear
foreach fip in `fips'{
	append using "$temp/fert_decomp_coll_nmarr_`fip'"
}

export delimited "$dir/Model/utilities/kid_fertility_probs_nmarr_coll_decomp_d.csv", replace novarn
restore


//hs, married
preserve
probit kid hc* if hc<3.0 & !coll & married [fw = perwt], robust 
local cons = _b[_cons]
local hc = _b[hc]
local hc2 = _b[hc2]
local hc3 = _b[hc3]
qui su kid if hc>3.0 & !coll & married [fw=perwt]
local cap = `r(mean)'

foreach fip in `fips'{
	clear
	set obs 1
	gen fips = `fip'
	gen cons = `cons'
	gen b1 = `hc'
	gen b2 = `hc2'
	gen b3 = `hc3'
	gen cap = `cap'
	gen mig = `mig'
	save "$temp/fert_decomp_ncoll_marr_`fip'", replace
}

clear
foreach fip in `fips'{
	append using "$temp/fert_decomp_ncoll_marr_`fip'"
}

export delimited "$dir/Model/utilities/kid_fertility_probs_marr_ncoll_decomp_d.csv", replace novarn
restore


//hs, not married
probit kid hc* if hc<3.0 & !coll & !married [fw = perwt], robust 
local cons = _b[_cons]
local hc = _b[hc]
local hc2 = _b[hc2]
local hc3 = _b[hc3]
qui su kid if hc>3.0 & !coll & !married [fw=perwt]
local cap = `r(mean)'

foreach fip in `fips'{
	clear
	set obs 1
	gen fips = `fip'
	gen cons = `cons'
	gen b1 = `hc'
	gen b2 = `hc2'
	gen b3 = `hc3'
	gen cap = `cap'
	gen mig = `mig'
	save "$temp/fert_decomp_ncoll_nmarr_`fip'", replace
}

clear
foreach fip in `fips'{
	append using "$temp/fert_decomp_ncoll_nmarr_`fip'"
}

export delimited "$dir/Model/utilities/kid_fertility_probs_nmarr_ncoll_decomp_d.csv", replace novarn





//end ofo dilfe